define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    function changeOneMenu() {
        var onemenu = $('input[name=onemenu]:checked').val();
        if (onemenu == 1) {
            //父菜单
            $('#fuji').hide();
        } else {
            $('#fuji').show();
        }
    };
    $(function () {
        changeOneMenu();
        $('body').delegate('input[name=onemenu]', 'click', function (){
            changeOneMenu();
        });
    });



    var Controller = {

        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/sys/menu/data',
                    add_url: '/sys/menu/add',
                    edit_url: '/sys/menu/edit',
                    del_url: '/sys/menu/del',
                    multi_url: 'data/multi.json',
                    "table": "t_resource"
                }
            });


            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id,weight',
                escape: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'id', title: 'ID',operate: false},
                        {field: 'title', title: __('Title'), align: 'left', align: 'left', formatter: Controller.api.formatter.title, operate: 'LIKE %...%'},
                        {field: 'url', title: __('路径'), align: 'left', align: 'left', formatter: Controller.api.formatter.title, operate: 'LIKE %...%'},
                        {field: 'icon', title: __('Icon'), formatter: Controller.api.formatter.icon,operate: false},
                        {field: 'pid', title: __('PID'), align: 'left'},
                        {
                            field: 'weight', title: __('Weigh'), formatter: function (value, row, index) {
                                return value;
                            },operate: false
                        },
                        {field: 'status', title: __('Status'), searchList: {"0": "禁用", "1": "启用"}, formatter: Table.api.formatter.status},
                        {field: 'onemenu', title: __('Ismenu'), align: 'center', searchList: {"2": "按钮", "1": "父菜单", "0": "子菜单"}, formatter: Controller.api.formatter.menu},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                pagination: true,
                search: true,
                commonSearch: true,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);//当内容渲染完成后
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                title: function (value, row, index) {
                    return !row.ismenu ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                name: function (value, row, index) {
                    return !row.ismenu ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                menu: function (value, row, index) {
                    if (value == 0) {
                        return "<a href='javascript:;' class='btn btn-reddit btn-xs btn-change' data-id='"
                            + row.id + "'>子菜单</a>"
                    }
                    if (value == 1) {
                        return "<a href='javascript:;' class='btn btn-info btn-xs btn-change' data-id='"
                            + row.id + "'>父菜单</a>"
                    }
                    if (value == 2) {
                        return "<a href='javascript:;' class='btn btn-danger btn-xs btn-change' data-id='"
                            + row.id + "'>按钮</a>"
                    }
                },
                icon: function (value, row, index) {
                    return '<i class="' + value + '"></i>';
                },
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-id="' + row['id'] + '" data-pid="' + row['pid'] + '" class="btn btn-xs '
                        + (row['haschild'] == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            },
            bindevent: function () {
                var iconlist = [];
                Form.api.bindevent($("form[role=form]"));
                $(document).on('click', ".btn-search-icon", function () {
                    if (iconlist.length == 0) {
                        $.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {
                            var exp = /fa-var-(.*):/ig;
                            var result;
                            while ((result = exp.exec(ret)) != null) {
                                iconlist.push(result[1]);
                            }
                            Layer.open({
                                type: 1,
                                area: ['460px', '300px'], //宽高
                                content: Template('chooseicontpl', {iconlist: iconlist})
                            });
                        });
                    } else {
                        Layer.open({
                            type: 1,
                            area: ['460px', '300px'], //宽高
                            content: Template('chooseicontpl', {iconlist: iconlist})
                        });
                    }
                });
                $(document).on('click', '#chooseicon ul li', function () {
                    $("input[name='icon']").val('fa fa-' + $(this).data("font"));
                    Layer.closeAll();
                });
                $(document).on('keyup', 'input.js-icon-search', function () {
                    $("#chooseicon ul li").show();
                    if ($(this).val() != '') {
                        $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
                    }
                });
            }
        }
    };
    return Controller;
});